import java.io.IOException;
import java.io.RandomAccessFile;

final class FileStore {
	private static final byte[] buffer = new byte[520];
	private final int storeId;
	private final RandomAccessFile dataFile;
	private final RandomAccessFile indexFile;

	public synchronized byte[] get(int i) {
		try {
			seek(indexFile, i * 6);
			int l;
			for (int j = 0; j < 6; j += l) {
				l = indexFile.read(FileStore.buffer, j, 6 - j);
				if (l == -1) {
					return null;
				}
			}
			int i1 = ((FileStore.buffer[0] & 0xff) << 16) + ((FileStore.buffer[1] & 0xff) << 8)
			+ (FileStore.buffer[2] & 0xff);
			int j1 = ((FileStore.buffer[3] & 0xff) << 16) + ((FileStore.buffer[4] & 0xff) << 8)
			+ (FileStore.buffer[5] & 0xff);
			// if(i1 < 0 || i1 > 0xffffff)
			// return null;
			if ((j1 <= 0) || (j1 > dataFile.length() / 520L)) {
				return null;
			}
			byte abyte0[] = new byte[i1];
			int k1 = 0;
			for (int l1 = 0; k1 < i1; l1++) {
				if (j1 == 0) {
					return null;
				}
				seek(dataFile, j1 * 520);
				int k = 0;
				int i2 = i1 - k1;
				if (i2 > 512) {
					i2 = 512;
				}
				int j2;
				for (; k < i2 + 8; k += j2) {
					j2 = dataFile.read(FileStore.buffer, k, (i2 + 8) - k);
					if (j2 == -1) {
						return null;
					}
				}
				int k2 = ((FileStore.buffer[0] & 0xff) << 8) + (FileStore.buffer[1] & 0xff);
				int l2 = ((FileStore.buffer[2] & 0xff) << 8) + (FileStore.buffer[3] & 0xff);
				int i3 = ((FileStore.buffer[4] & 0xff) << 16) + ((FileStore.buffer[5] & 0xff) << 8)
				+ (FileStore.buffer[6] & 0xff);
				int j3 = FileStore.buffer[7] & 0xff;
				if ((k2 != i) || (l2 != l1) || (j3 != storeId)) {
					return null;
				}
				if ((i3 < 0) || (i3 > dataFile.length() / 520L)) {
					return null;
				}
				for (int k3 = 0; k3 < i2; k3++) {
					abyte0[k1++] = FileStore.buffer[k3 + 8];
				}
				j1 = i3;
			}
			return abyte0;
		} catch (IOException _ex) {
			return null;
		}
	}

	public synchronized boolean store(int i, byte abyte0[], int j) {
		boolean flag = put(true, j, i, abyte0);
		if (!flag) {
			flag = put(false, j, i, abyte0);
		}
		return flag;
	}

	private synchronized boolean put(boolean flag, int index, int length, byte data[]) {
		try {
			int pos;
			if (flag) {
				seek(indexFile, index * 6);
				int k1;
				for (int i1 = 0; i1 < 6; i1 += k1) {
					k1 = indexFile.read(FileStore.buffer, i1, 6 - i1);
					if (k1 == -1) {
						return false;
					}
				}
				pos = ((FileStore.buffer[3] & 0xff) << 16) + ((FileStore.buffer[4] & 0xff) << 8)
				+ (FileStore.buffer[5] & 0xff);
				if ((pos <= 0) || (pos > dataFile.length() / 520L)) {
					return false;
				}
			} else {
				pos = (int) ((dataFile.length() + 519L) / 520L);
				if (pos == 0) {
					pos = 1;
				}
			}
			FileStore.buffer[0] = (byte) (length >> 16);
			FileStore.buffer[1] = (byte) (length >> 8);
			FileStore.buffer[2] = (byte) length;
			FileStore.buffer[3] = (byte) (pos >> 16);
			FileStore.buffer[4] = (byte) (pos >> 8);
			FileStore.buffer[5] = (byte) pos;
			seek(indexFile, index * 6);
			indexFile.write(FileStore.buffer, 0, 6);
			int j1 = 0;
			for (int l1 = 0; j1 < length; l1++) {
				int i2 = 0;
				if (flag) {
					seek(dataFile, pos * 520);
					int j2;
					int l2;
					for (j2 = 0; j2 < 8; j2 += l2) {
						l2 = dataFile.read(FileStore.buffer, j2, 8 - j2);
						if (l2 == -1) {
							break;
						}
					}
					if (j2 == 8) {
						int i3 = ((FileStore.buffer[0] & 0xff) << 8) + (FileStore.buffer[1] & 0xff);
						int j3 = ((FileStore.buffer[2] & 0xff) << 8) + (FileStore.buffer[3] & 0xff);
						i2 = ((FileStore.buffer[4] & 0xff) << 16)
						+ ((FileStore.buffer[5] & 0xff) << 8) + (FileStore.buffer[6] & 0xff);
						int k3 = FileStore.buffer[7] & 0xff;
						if ((i3 != index) || (j3 != l1) || (k3 != storeId)) {
							return false;
						}
						if ((i2 < 0) || (i2 > dataFile.length() / 520L)) {
							return false;
						}
					}
				}
				if (i2 == 0) {
					flag = false;
					i2 = (int) ((dataFile.length() + 519L) / 520L);
					if (i2 == 0) {
						i2++;
					}
					if (i2 == pos) {
						i2++;
					}
				}
				if (length - j1 <= 512) {
					i2 = 0;
				}
				FileStore.buffer[0] = (byte) (index >> 8);
				FileStore.buffer[1] = (byte) index;
				FileStore.buffer[2] = (byte) (l1 >> 8);
				FileStore.buffer[3] = (byte) l1;
				FileStore.buffer[4] = (byte) (i2 >> 16);
				FileStore.buffer[5] = (byte) (i2 >> 8);
				FileStore.buffer[6] = (byte) i2;
				FileStore.buffer[7] = (byte) storeId;
				seek(dataFile, pos * 520);
				dataFile.write(FileStore.buffer, 0, 8);
				int k2 = length - j1;
				if (k2 > 512) {
					k2 = 512;
				}
				dataFile.write(data, j1, k2);
				j1 += k2;
				pos = i2;
			}
			return true;
		} catch (IOException _ex) {
			return false;
		}
	}

	private synchronized void seek(RandomAccessFile randomaccessfile, int pos) throws IOException {
		if ((pos < 0) || (pos > 0x3c00000)) {
			System.out.println("Badseek - pos:" + pos + " len:" + randomaccessfile.length());
			pos = 0x3c00000;
			try {
				Thread.sleep(1000L);
			} catch (Exception _ex) {
			}
		}
		randomaccessfile.seek(pos);
	}

	public FileStore(RandomAccessFile dataFile, RandomAccessFile indexFile, int storeId) {
		this.storeId = storeId;
		this.dataFile = dataFile;
		this.indexFile = indexFile;
	}
}
